package com.caration.shell.workspace2D;

import java.util.ArrayList;
import java.util.HashSet;

import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.graphics.Canvas;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.ViewParent;

import com.caration.shell.Home;
import com.caration.shell.item.AppWidgetInfo;
import com.caration.shell.item.ItemInfo;
import com.caration.shell.utils.Reflect;
import com.caration.shell.workspace3D.ShellRenderHost;

public class Workspace extends ViewGroup {
	private boolean a = true;
	private ShellMenuInfo mMenuInfo;
	private int[] c = null;
	private float d;
	private float e;
	private int f = 0;
	private View.OnLongClickListener mLongClickListener;
	private Context mContext;
	private DragController mController;
	private int[] j = new int[2];
	private int[] k = new int[2];
	private boolean l = true;
	private boolean m = false;
	private MotionEvent mMotionEvent;
	private ak o;
	private VelocityTracker mVelocityTracker;
	private int MIN_VELOCITY;
	private int MAX_VELOCITY;
	private boolean s = false;
	private Runnable t;

	public Workspace(Context context) {
		super(context);
		mContext = context;
		setHapticFeedbackEnabled(false);
		Reflect.setMotionEventSplittingDisable(this);
		ViewConfiguration viewConfiguration = ViewConfiguration.get(context);
		MIN_VELOCITY = viewConfiguration.getScaledMinimumFlingVelocity();
		MAX_VELOCITY = viewConfiguration.getScaledMaximumFlingVelocity();
	}

	private static ScreenLayout getScreenLayoutParent(View view) {
		ViewParent parent = view.getParent();
		if ((parent == null) || (!(parent instanceof ScreenLayout)))
			return null;
		else
			return (ScreenLayout) parent;
	}

	public static ShellMenuInfo c() {
		Object localObject = ShellRenderHost.h().h().l();
		if (localObject != null)
			localObject = ((ScreenLayout) localObject).c();
		else
			localObject = null;
		return (ShellMenuInfo) localObject;
	}

	private ScreenLayout d() {
		return this.o.h().l();
	}

	public final void a(int paramInt1, int paramInt2) {
		if (this.c != null) {
			this.c[0] = paramInt1;
			this.c[1] = paramInt2;
		}
	}

	public final void a(View view, int paramInt1, int paramInt2, int paramInt3,
			int paramInt4, boolean paramBoolean) {
		a(view, d(), paramInt1, paramInt2, paramInt3, paramInt4, paramBoolean);
	}

	public final void a(View view, ScreenLayout paramScreenLayout,
			int paramInt1, int paramInt2, int paramInt3, int paramInt4,
			boolean paramBoolean) {
		if (paramScreenLayout != null) {
			ScreenLayout.LayoutParams localLayoutParams1 = (ScreenLayout.LayoutParams) view
					.getLayoutParams();
			ScreenLayout.LayoutParams localLayoutParams2;
			if (localLayoutParams1 != null) {
				localLayoutParams1.a = paramInt1;
				localLayoutParams1.b = paramInt2;
				localLayoutParams1.c = paramInt3;
				localLayoutParams1.d = paramInt4;
				localLayoutParams2 = localLayoutParams1;
			} else {
				localLayoutParams2 = new ScreenLayout.LayoutParams(paramInt1,
						paramInt2, paramInt3, paramInt4);
			}
			int i1;
			if (!paramBoolean)
				i1 = -1;
			else
				i1 = 0;
			paramScreenLayout.a(view, i1, localLayoutParams2);
			view.setHapticFeedbackEnabled(false);
			view.setOnLongClickListener(mLongClickListener);
			if ((view instanceof DropTarget))
				mController.addTarget((DropTarget) view);
		}
	}

	public final void a(View view, boolean paramBoolean) {
		if (!paramBoolean) {
			if (mMenuInfo != null)
				mMenuInfo.f.c(mMenuInfo.a);
		} else if ((view != this) && (mMenuInfo != null)) {
			ShellMenuInfo localp = mMenuInfo;
			localp.f.b(localp.a);
			if ((localp.a instanceof DropTarget))
				mController.removeTarget((DropTarget) localp.a);
		}
		mMenuInfo = null;
	}

	//a
	public final void removeWidget(AppWidgetInfo widgetInfo) {
		((ScreenLayout) widgetInfo.mHostView.getParent())
				.b(widgetInfo.mHostView);
		if ((widgetInfo.mHostView instanceof DropTarget))
			mController.removeTarget((DropTarget) widgetInfo.mHostView);
	}

	//a
	public final void setController(DragController controller) {
		mController = controller;
		mController.a(this);
	}

	public final void a(h paramh) {
		int i1 = 1;
		ScreenLayout localScreenLayout1 = d();
		if ((paramh == this) && (mMenuInfo != null)) {
			View localView = mMenuInfo.a;
			int i2 = d().b();
			ItemInfo locala = (ItemInfo) localView.getTag();
			ScreenLayout.LayoutParams localLayoutParams = (ScreenLayout.LayoutParams) localView
					.getLayoutParams();
			if (i2 == mMenuInfo.f.b()) {
				mMenuInfo.f.b(localLayoutParams);
				i1 = 0;
			} else {
				ScreenLayout localScreenLayout2 = a(localView);
				if (localScreenLayout2 == null)
					mMenuInfo.f.b(localView);
				else
					localScreenLayout2.b(localView);
				localLayoutParams.a = this.c[0];
				localLayoutParams.b = this.c[i1];
				localScreenLayout1.a(localView);
			}
			localScreenLayout1.a(localView, this.c);
			if (i1 == 0)
				mMenuInfo.f.a(localLayoutParams);
			bb.a(mContext, locala, i2, localLayoutParams.a, localLayoutParams.b);
		}
	}

	public final void a(ShellMenuInfo paramp) {
		View localView = paramp.a;
		if ((localView.isInTouchMode()) && (d() != null)) {
			mMenuInfo = paramp;
			Object localObject = a(localView);
			if (localObject == null)
				mMenuInfo.f = d();
			else
				mMenuInfo.f = ((ScreenLayout) localObject);
			d().d(localView);
			b localb = mController;
			localObject = localView.getTag();
			localb.a(localView, this, localObject);
		}
	}

	public final void a(ak paramak) {
		this.o = paramak;
	}

	public final void a(ArrayList paramArrayList) {
		int i1 = 0;
		int i4 = getChildCount();
		AppWidgetManager localAppWidgetManager = AppWidgetManager
				.getInstance(getContext());
		HashSet localHashSet = new HashSet();
		int i3 = paramArrayList.size();
		for (int i2 = 0; i2 < i3; i2++)
			localHashSet.add(((k) paramArrayList.get(i2)).c.getPackageName());
		while (i1 < i4) {
			post(new u(this, (ScreenLayout) getChildAt(i1),
					localAppWidgetManager, localHashSet));
			i1++;
		}
	}

	public final boolean a() {
		return this.l;
	}

	public final boolean a(int paramInt1, int paramInt2, int paramInt3,
			int paramInt4) {
		Object localObject = d();
		int i1;
		if (localObject != null) {
			ShellMenuInfo localp = mMenuInfo;
			int i3;
			if (localp != null)
				i3 = localp.d;
			else
				i3 = 1;
			int i2;
			if (localp != null)
				i2 = localp.e;
			else
				i2 = 1;
			if (localp != null) {
				float[] arrayOfFloat = new float[2];
				ScreenLayout.a(paramInt1 - paramInt3, paramInt2 - paramInt4,
						arrayOfFloat);
				if (localp.f != localObject)
					localObject = ((ScreenLayout) localObject).a(
							arrayOfFloat[0], arrayOfFloat[1], i3, i2);
				else
					localObject = ((ScreenLayout) localObject).a(
							arrayOfFloat[0], arrayOfFloat[1], i3, i2, localp.b,
							localp.c, localp.d, localp.e);
				if (localObject == null) {
					i1 = 0;
				} else {
					this.c = i1;
					i1 = 1;
				}
			} else {
				i1 = 0;
			}
		} else {
			i1 = 0;
		}
		return i1;
	}

	public final void b() {
		this.m = true;
		this.l = false;
	}

	protected final void dispatchDraw(Canvas paramCanvas) {
		super.dispatchDraw(paramCanvas);
		if (this.t != null) {
			this.t.run();
			this.t = null;
		}
	}

	public final boolean dispatchTouchEvent(MotionEvent paramMotionEvent) {
		return super.dispatchTouchEvent(paramMotionEvent);
	}

	protected final void onAttachedToWindow() {
		super.onAttachedToWindow();
		computeScroll();
		mController.a(getWindowToken());
	}

	public final boolean onInterceptTouchEvent(MotionEvent paramMotionEvent)
	  {
	    int i1 = 1;
	    int i2 = paramMotionEvent.getAction();
	    if ((i2 == 2) && (this.f == i1));
	    label64: int i5;
	    int i3;
	    while (true)
	    {
	      return i1;
	      switch (i2 & 0xFF)
	      {
	      case 4:
	      default:
	        if (this.f == 0)
	          i1 = 0;
	        break;
	      case 2:
	        float f3 = paramMotionEvent.getX();
	        float f1 = paramMotionEvent.getY();
	        i5 = (int)Math.abs(f3 - this.d);
	        i3 = (int)Math.abs(f1 - this.e);
	        if (i5 * i5 + i3 * i3 <= com.censivn.C3DEngine.b.b.b)
	          break label374;
	        if (i5 <= i3)
	          break label369;
	        i3 = i1;
	        label134: i5 = i3;
	        i3 = i1;
	      case 0:
	      case 1:
	      case 3:
	      case 5:
	      }
	    }
	    while (true)
	    {
	      while (true)
	      {
	        if ((i5 == 0) && (i3 == 0))
	          break label64;
	        if (i5 != 0)
	          this.f = i1;
	        if (!this.l)
	          break label64;
	        this.l = false;
	        if (d() == null)
	          break label64;
	        d().cancelLongPress();
	        break label64;
	        Home.c().a(paramMotionEvent.getX(), paramMotionEvent.getY());
	        Object localObject1 = com.censivn.C3DEngine.h.a.a(paramMotionEvent);
	        try
	        {
	          localObject1 = com.censivn.C3DEngine.a.f().a(localObject1[0], localObject1[1]);
	          localObject1 = localObject1;
	          if (localObject1 != null)
	            break;
	          localObject1 = new t(this);
	          com.censivn.C3DEngine.a.a();
	          w.c((Runnable)localObject1);
	          mMotionEvent = MotionEvent.obtain(paramMotionEvent);
	          float f2 = paramMotionEvent.getX();
	          float f4 = paramMotionEvent.getY();
	          this.d = f2;
	          this.e = f4;
	          this.l = i1;
	          this.m = false;
	          this.s = false;
	          this.f = 0;
	          i1 = 0;
	        }
	        catch (Exception localException)
	        {
	          while (true)
	            Object localObject2 = null;
	        }
	      }
	      if ((this.f != i1) && (d() != null) && (!d().f()))
	        getLocationOnScreen(this.j);
	      this.f = 0;
	      this.l = false;
	      break label64;
	      if (this.f != 0)
	        break label64;
	      this.s = i1;
	      this.f = i1;
	      break label64;
	      break;
	      label369: int i4 = 0;
	      break label134;
	      label374: i4 = 0;
	      int i6 = 0;
	    }
	  }

	protected final void onLayout(boolean paramBoolean, int paramInt1,
			int paramInt2, int paramInt3, int paramInt4) {
		int i2 = getChildCount();
		for (int i1 = 0; i1 < i2; i1++) {
			View localView = getChildAt(i1);
			if (localView.getVisibility() == 8)
				continue;
			localView.layout(0, 0, localView.getMeasuredWidth(),
					localView.getMeasuredHeight());
		}
	}

	protected final void onMeasure(int paramInt1, int paramInt2) {
		super.onMeasure(paramInt1, paramInt2);
		if (View.MeasureSpec.getMode(paramInt1) == 1073741824) {
			if (View.MeasureSpec.getMode(paramInt2) == 1073741824) {
				int i2 = getChildCount();
				for (int i1 = 0; i1 < i2; i1++)
					getChildAt(i1).measure(paramInt1, paramInt2);
				if (this.a) {
					setHorizontalScrollBarEnabled(false);
					setHorizontalScrollBarEnabled(true);
					this.a = false;
				}
				return;
			}
			throw new IllegalStateException(
					"Workspace3D can only be used in EXACTLY mode.");
		}
		throw new IllegalStateException(
				"Workspace3D can only be used in EXACTLY mode.");
	}

	public final boolean onTouchEvent(MotionEvent paramMotionEvent) {
		if (mVelocityTracker == null)
			mVelocityTracker = VelocityTracker.obtain();
		mVelocityTracker.addMovement(paramMotionEvent);
		if (!this.s)
			switch (paramMotionEvent.getAction()) {
			default:
				break;
			case 1:
				if (this.f == 1) {
					VelocityTracker localVelocityTracker = mVelocityTracker;
					localVelocityTracker.computeCurrentVelocity(1000,
							MAX_VELOCITY);
					float f1 = localVelocityTracker.getYVelocity();
					float f2 = localVelocityTracker.getXVelocity();
					if ((Math.abs(f1) > MIN_VELOCITY)
							|| (Math.abs(f2) > MIN_VELOCITY))
						l.b().c(mMotionEvent, paramMotionEvent, f2, f1);
					if (mVelocityTracker != null) {
						mVelocityTracker.recycle();
						mVelocityTracker = null;
					}
				}
				l.b().j(paramMotionEvent);
				break;
			case 2:
				if ((this.f != 1) || (this.m))
					break;
				l.b().a(mMotionEvent, paramMotionEvent);
				break;
			}
		else
			com.tsf.shell.action.c.a(paramMotionEvent);
		return false;
	}

	public final void setOnLongClickListener(
			View.OnLongClickListener longClickListener) {
		mLongClickListener = longClickListener;
	}
}
